package com.lamp.main;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TabHost;
import android.widget.TextView;

import com.lamp.JsonClass.FriendList;
import com.lamp.JsonClass.FriendStatus;
import com.lamp.JsonClass.MessageListStatus;
import com.lamp.connection.ConnectionManager;
import com.lamp.connection.RequestManager;
import com.lamp.downloadimage.BitmapCache;
import com.lamp.tabgroups.FeedGroup;

public class TabBarActivity extends TabActivity implements OnClickListener {
	Button home;
	Button friend;
	Button sms;
	LinearLayout hSField;
	LinearLayout headerField;
	TextView header;
	private ImageButton newSmsBtn;
	private MessageListStatus messageListStatus = new MessageListStatus();
	private FriendStatus friendRequestStatus;
	private ListView listView;
	private static final BitmapCache bitmapCache = new BitmapCache();
	RequestManager _requestManager;
	private ImageButton newB;
	private int REL_SWIPE_MIN_DISTANCE;
	private int REL_SWIPE_MAX_OFF_PATH;
	private int REL_SWIPE_THRESHOLD_VELOCITY;
	private int ingorePosition = -1;
	TabHost tabHost;
	public static final int MENU_LOGOUT = Menu.FIRST + 1;
	public static final int MENU_ABOUT = Menu.FIRST + 2;
	public static final int MENU_CAP = Menu.FIRST + 3;
	public static final int MENU_REMOVE = Menu.FIRST + 4;

	private LinearLayout center_btn;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		registerForContextMenu(findViewById(android.R.id.tabhost));
		setTabs();
		center_btn = (LinearLayout) findViewById(R.id.centerButton);
		// header = (TextView) findViewById(R.id.smsheader);
		// home = (Button) findViewById(R.id.homeBtn);
		// home.setOnClickListener(this);
		// friend = (Button) findViewById(R.id.homeFriBtn);
		// friend.setOnClickListener(this);
		//
		// sms = (Button) findViewById(R.id.homeSmsBtn);
		// sms.setOnClickListener(this);
		// newSmsBtn = (ImageButton) findViewById(R.id.newSmsBtn);
		// newSmsBtn.setOnClickListener(this);
		// hSField = (LinearLayout) findViewById(R.id.hSMessageField);
		// hSField.setOnTouchListener(new OnTouchListener() {
		//
		// public boolean onTouch(View v, MotionEvent event) {
		// hSField.setAnimation(AnimationUtils.loadAnimation(
		// TabBarActivity.this, R.anim.pop_up_sms_out));
		// hSField.setVisibility(View.GONE);
		// FeedGroup.group.clickF = false;
		// FeedGroup.group.clickM = false;
		// return false;
		// }
		// });
		// headerField = (LinearLayout) findViewById(R.id.ppHeader);
		//
		// listView = (ListView) findViewById(R.id.messageListView);
		//
		// DisplayMetrics dm = getResources().getDisplayMetrics();
		// REL_SWIPE_MIN_DISTANCE = (int) (120.0f * dm.densityDpi / 160.0f +
		// 0.5);
		// REL_SWIPE_MAX_OFF_PATH = (int) (250.0f * dm.densityDpi / 160.0f +
		// 0.5);
		// REL_SWIPE_THRESHOLD_VELOCITY = (int) (200.0f * dm.densityDpi / 160.0f
		// + 0.5);
		// messageListStatus =
		// LampLoginActivity.lampLogin.getMessageListStatus();
		// this.friendRequestStatus = LampLoginActivity.lampLogin
		// .getNewFriendStatus();
		// final GestureDetector gestureDetector = new GestureDetector(
		// new MyGestureDetector());
		// View.OnTouchListener gestureListener = new View.OnTouchListener() {
		// public boolean onTouch(View v, MotionEvent event) {
		// return gestureDetector.onTouchEvent(event);
		// }
		// };
		// listView.setOnTouchListener(gestureListener);
	}

	public void visibleTabWedgit() {
		tabHost.getTabWidget().setVisibility(View.VISIBLE);
		center_btn.setVisibility(View.VISIBLE);
	}

	public void goneTabWedgit() {

		tabHost.getTabWidget().setVisibility(View.GONE);
		center_btn.setVisibility(View.GONE);
	}

	private void setTabs() {
		addTab(getString(R.string.tab1), R.drawable.tab_feed, FeedGroup.class);
		addTab(getString(R.string.tab2), R.drawable.tab_search, FeedGroup.class);
		addTab(getString(R.string.tab3), R.drawable.tab_center, FeedGroup.class);
		addTab(getString(R.string.tab4), R.drawable.tab_nearby, FeedGroup.class);
		addTab(getString(R.string.tab5), R.drawable.tab_mine, FeedGroup.class);
	}

	private void addTab(String labelId, int drawableId, Class<?> c) {
		tabHost = getTabHost();
		Intent intent = new Intent(this, c);
		TabHost.TabSpec spec = tabHost.newTabSpec("tab" + labelId);

		View tabIndicator = LayoutInflater.from(this).inflate(
				R.layout.tab_indicator, getTabWidget(), false);
		TextView title = (TextView) tabIndicator.findViewById(R.id.title);
		title.setText(labelId);
		ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
		icon.setBackgroundResource(drawableId);

		spec.setIndicator(tabIndicator);
		spec.setContent(intent);
		tabHost.addTab(spec);
	}

	public int getTabIndex() {
		return tabHost.getCurrentTab();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	protected void onStop() {
		super.onStop();
	}

	@Override
	public void onBackPressed() {
		FeedGroup.group.back();
	};

	// public void adapterSms() {
	// Log.d("tester",
	// String.valueOf(messageListStatus.getMessageList().size() + ""));
	// final ArrayAdapter<MessageList> messageAdapter = new MessageListAdapter(
	// this, messageListStatus.getMessageList(), bitmapCache);
	// listView.setAdapter(messageAdapter);
	// }
	//
	// public void adapterFriReq() {
	// Log.d("tester",
	// String.valueOf(messageListStatus.getMessageList().size() + ""));
	// ArrayAdapter<FriendList> friendRequestAdapter = new FriendListAdapter(
	// TabBarActivity.this, friendRequestStatus.getFriendList(),
	// bitmapCache, 2);
	// listView.setAdapter(friendRequestAdapter);
	// }
	//
	// private void myOnItemClick(int position) {
	// View rowView = listView.getChildAt(position);
	// if (rowView != null) {
	// Intent intent = null;
	// FeedGroup.group.realClick = false;
	// FeedGroup.group.popUpPosition();
	// friend.setClickable(false);
	// sms.setClickable(false);
	// if (FeedGroup.group.clickM) {
	// ((MessageList) listView.getAdapter().getItem(position))
	// .setStatusNew(false);
	// intent = new Intent(this, GetMessageDetList.class);
	// intent.putExtra("smsCode", ((MessageList) listView.getAdapter()
	// .getItem(position)).getMessageCode());
	// } else {
	// intent = new Intent(TabBarActivity.this, GetUser.class);
	// intent.putExtra("userId", ((FriendList) listView.getAdapter()
	// .getItem(position)).getUserId());
	// intent.putExtra("isRequest", ((FriendListAdapter) listView
	// .getAdapter()).getRequest());
	// intent.putExtra("groupType", 5);
	// }
	// hSField.setAnimation(AnimationUtils.loadAnimation(this,
	// R.anim.out_to_left));
	// hSField.setVisibility(View.GONE);
	// FeedGroup.group.goTo(intent);
	// }
	// }
	//
	// private void onLTRFling() {
	// View rowView = listView.getChildAt(ingorePosition);
	// if (rowView != null) {
	// Button ignore = (Button) rowView.findViewById(R.id.ignoreButton);
	// ignore.setAnimation(AnimationUtils.loadAnimation(this,
	// R.anim.out_to_right));
	// ignore.setVisibility(View.GONE);
	// ingorePosition = -1;
	// }
	// }
	//
	// private void deleteMessageGroup(int position) {
	// ConnectionManager _connectionManager;
	// RequestManager _requestManager;
	// _requestManager = new RequestManager(this);
	// _connectionManager = new ConnectionManager(this);
	//
	// _connectionManager.setCurrentActivity(this);
	// _requestManager.setCurrentActivity(this);
	//
	// _requestManager.setConnectionManager(_connectionManager);
	// _connectionManager.setRequestManager(_requestManager);
	//
	// MessageList removeMessage = (MessageList) listView.getAdapter()
	// .getItem(position);
	// List<NameValuePair> params1 = new ArrayList<NameValuePair>();
	// params1.add(new BasicNameValuePair("Token", LoginActivity.Token));
	// params1.add(new BasicNameValuePair("MessageCode", String
	// .valueOf(removeMessage.getMessageCode())));
	// _connectionManager.setParams(params1);
	// _connectionManager
	// .setUrl("http://www.lamp.mn/lamp/webservices/deleteMessageGroup.php");
	// _requestManager
	// .setRequestType(RequestManager.REQUEST_DELETEMESSAGEGROUP);
	// _requestManager.start();
	// messageListStatus.setTotal(messageListStatus.getTotal() - 1);
	// }
	//
	// private void onRTLFling(final int position) {
	// final View rowView = listView.getChildAt(position);
	// if (rowView != null) {
	// if (ingorePosition != -1) {
	// onLTRFling();
	// } else {
	// ingorePosition = position;
	// if (FeedGroup.group.clickM) {
	// Button ignore = (Button) rowView
	// .findViewById(R.id.ignoreButton);
	// ignore.setAnimation(AnimationUtils.loadAnimation(this,
	// R.anim.in_from_right));
	// ignore.setVisibility(View.VISIBLE);
	// ignore.setOnClickListener(new View.OnClickListener() {
	// public void onClick(View v) {
	// ((MessageListAdapter) listView.getAdapter())
	// .removeIt(position);
	// deleteMessageGroup(position);
	// ingorePosition = -1;
	// LampLoginActivity.lampLogin.user.adapterOther();
	// }
	// });
	// } else {
	// Button ignore = (Button) rowView
	// .findViewById(R.id.ignoreButton);
	// ignore.setAnimation(AnimationUtils.loadAnimation(this,
	// R.anim.in_from_right));
	// ignore.setVisibility(View.VISIBLE);
	// ignore.setOnClickListener(new View.OnClickListener() {
	// public void onClick(View v) {
	// ((FriendListAdapter) listView.getAdapter())
	// .removeIt(position);
	//
	// ignoreFriendRequest(position);
	// friendRequestStatus.setTotal(friendRequestStatus
	// .getTotal() - 1);
	//
	// onLTRFling();
	// LampLoginActivity.lampLogin.user.adapterOther();
	// }
	// });
	// }
	// }
	// }
	// }
	//
	// class MyGestureDetector extends SimpleOnGestureListener {
	//
	// @Override
	// public boolean onSingleTapUp(MotionEvent e) {
	// ListView lv = listView;
	// int pos = lv.pointToPosition((int) e.getX(), (int) e.getY());
	// myOnItemClick(pos);
	// return false;
	// }
	//
	// @Override
	// public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
	// float velocityY) {
	// if (Math.abs(e1.getY() - e2.getY()) > REL_SWIPE_MAX_OFF_PATH)
	// return false;
	// if (e1.getX() - e2.getX() > REL_SWIPE_MIN_DISTANCE
	// && Math.abs(velocityX) > REL_SWIPE_THRESHOLD_VELOCITY) {
	// {
	// ListView lv = listView;
	// int pos = lv.pointToPosition((int) e1.getX(),
	// (int) e1.getY());
	// onRTLFling(pos);
	// }
	// } else if (e2.getX() - e1.getX() > REL_SWIPE_MIN_DISTANCE
	// && Math.abs(velocityX) > REL_SWIPE_THRESHOLD_VELOCITY) {
	// {
	// ListView lv = listView;
	// int pos = lv.pointToPosition((int) e1.getX(),
	// (int) e1.getY());
	// onRTLFling(pos);
	// }
	// }
	// return false;
	// }
	// }

	public void onClick(View v) {
		switch (v.getId()) {

		case R.id.homeCenterlt:
			FeedGroup.group.clear();
			break;
		// case R.id.homeSmsBtn:
		// Intent intent = new Intent(TabBarActivity.this,
		// GetMessageDetList.class);
		// hSField.setAnimation(AnimationUtils.loadAnimation(this,
		// R.anim.out_to_left));
		// hSField.setVisibility(View.GONE);
		// friend.setClickable(false);
		// sms.setClickable(false);
		// FeedGroup.group.goTo(intent);
		// break;
		case R.id.homeFriBtn:
			FeedGroup.group.realClick = true;
			if (!FeedGroup.group.clickF) {
				newSmsBtn.setVisibility(View.INVISIBLE);
				headerField.setBackgroundResource(R.drawable.nfeed_btn);
				FeedGroup.group.clickF = true;
				// adapterFriReq();
				header.setText(R.string.req);
				if (!FeedGroup.group.clickM) {
					hSField.setAnimation(AnimationUtils.loadAnimation(this,
							R.anim.pop_up_sms_in));
					hSField.setVisibility(View.VISIBLE);
				}
				FeedGroup.group.clickM = false;
			} else {
				FeedGroup.group.clickF = false;
				FeedGroup.group.realClick = false;
				hSField.setAnimation(AnimationUtils.loadAnimation(
						TabBarActivity.this, R.anim.pop_up_sms_out));
				hSField.setVisibility(View.GONE);
			}
			break;
		case R.id.homeSmsBtn:

			FeedGroup.group.realClick = true;
			if (!FeedGroup.group.clickM) {
				newSmsBtn.setVisibility(View.VISIBLE);
				// hSMessageField.setVisibility(View.VISIBLE);
				// Intent getSms = new Intent(TabBarActivity.this,
				// GetMessageLists.class);
				headerField.setBackgroundResource(R.drawable.nmessage_btn);
				FeedGroup.group.clickM = true;
				header.setText(R.string.sms);
				// FeedGroup.group.goTo(getSms, true);

				// newB = (ImageButton) getParent().getParent().findViewById(
				// R.id.homeModul);
				// newB.setOnClickListener(new View.OnClickListener() {
				// public void onClick(View v) {
				// Intent intent = new Intent(TabBarActivity.this,
				// GetMessageDetList.class);
				// FeedGroup.group.goTo(intent);
				// }
				// });

				// adapterSms();
				// Long-click still works in the usual way.
				listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
					public boolean onItemLongClick(AdapterView<?> parent,
							View view, int position, long id) {

						return true;
					}
				});
				if (!FeedGroup.group.clickF) {
					hSField.setAnimation(AnimationUtils.loadAnimation(this,
							R.anim.pop_up_sms_in));
					hSField.setVisibility(View.VISIBLE);
				}
				FeedGroup.group.clickF = false;
			} else {
				// FeedGroup.group.back_up();

				FeedGroup.group.realClick = false;
				FeedGroup.group.clickM = false;
				hSField.setAnimation(AnimationUtils.loadAnimation(
						TabBarActivity.this, R.anim.pop_up_sms_out));
				hSField.setVisibility(View.GONE);
			}
			break;
		}

	}

	// @Override
	// public boolean onCreateOptionsMenu(Menu menu) {
	// menu.add(Menu.NONE, MENU_LOGOUT, Menu.NONE, "Logout").setIcon(
	// R.drawable.ic_menu_add);
	// menu.add(Menu.NONE, MENU_ABOUT, Menu.NONE, "About").setIcon(
	// R.drawable.ic_menu_refresh);
	//
	// return (super.onCreateOptionsMenu(menu));
	// }
	//
	// @Override
	// public boolean onOptionsItemSelected(MenuItem item) {
	// switch (item.getItemId()) {
	// case MENU_LOGOUT:
	// finish();
	// return (true);
	//
	// case MENU_ABOUT:
	// return (true);
	// }
	//
	// return (super.onOptionsItemSelected(item));
	// }

	private void ignoreFriendRequest(int position) {
		ConnectionManager _connectionManager;
		RequestManager _requestManager;
		_requestManager = new RequestManager(this);
		_connectionManager = new ConnectionManager(this);

		_connectionManager.setCurrentActivity(this);
		_requestManager.setCurrentActivity(this);

		_requestManager.setConnectionManager(_connectionManager);
		_connectionManager.setRequestManager(_requestManager);

		FriendList removeMessage = (FriendList) listView.getAdapter().getItem(
				position);
		List<NameValuePair> params1 = new ArrayList<NameValuePair>();
		params1.add(new BasicNameValuePair("Token", LoginActivity.Token));
		params1.add(new BasicNameValuePair("UserId", String
				.valueOf(removeMessage.getUserId())));
		_connectionManager.setParams(params1);
		_connectionManager
				.setUrl("http://www.lamp.mn/lamp/webservices/ignoreFriendRequest.php");
		_requestManager
				.setRequestType(RequestManager.REQUEST_IGNOREFRIENDREQUEST);
		_requestManager.start();
	}

	@Override
	public void finish() {
		// GetUser.user.userId = 0;
		super.finish();
	}

}